package com.guet.niit.Login.controller.admin;

import com.guet.niit.Admin.dao.AgentDaolmpl;
import com.guet.niit.Admin.dao.ChargingDaolmpl;
import com.guet.niit.Admin.dao.ChargingPileDaolmpl;
import com.guet.niit.Admin.dao.UserDaolmpl;
import com.guet.niit.Admin.pojo.Agent;
import com.guet.niit.Admin.pojo.Charging;
import com.guet.niit.Admin.pojo.ChargingPile;
import com.guet.niit.Admin.pojo.User;
import com.guet.niit.Login.pojo.StationData;
import com.guet.niit.Login.pojo.StationPileData;
import com.guet.niit.util.SnowFlakeUtil;
import com.guet.niit.util.adminSwitchoverMenu;
import com.guet.niit.util.exitLogin;
import com.sun.javafx.stage.StageHelper;
import javafx.beans.property.*;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
import javafx.scene.text.Text;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.ResourceBundle;

public class ModifyChargingPileDataFxController implements Initializable {
    public static ModifyChargingPileDataFxController modifyChargingPileDataFxController;

    @FXML
    private MenuItem stationManage;

    @FXML
    private MenuItem agentManage;

    @FXML
    private MenuItem userManage;

    @FXML
    private MenuItem mainMenu;

    @FXML
    private MenuItem exit;

    @FXML
    private ChoiceBox<String> m_stationName;

    @FXML
    private TextField m_servicePower;

    @FXML
    private TextField m_chargingType;

    @FXML
    private TextField m_serviceCharge;

    @FXML
    private Text modifyType;

    @FXML
    private Text oriStationPileId;

    @FXML
    private Text oriStationName;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        modifyChargingPileDataFxController = this;
    }

    /**
     * 通过菜单子项进入充电站管理
     * @param actionEvent
     */
    public void enterStationManageByMenuItem(ActionEvent actionEvent) {
        //弹出确认对话框询问用户
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
        alert.setHeaderText(null);
        alert.setContentText("您确定要放弃修改数据吗？");
        Optional<ButtonType> result = alert.showAndWait();
        //用户选择放弃修改数据
        if (result.get()==ButtonType.OK) {
            //进入充电站管理
            String text = stationManage.getText();
            adminSwitchoverMenu.backtrack(ModifyChargingPileDataFxController.class,text);
            //调用dao层查询充电站表
            ChargingDaolmpl chargingDao = new ChargingDaolmpl();
            List<Charging> stations = chargingDao.chargingInquire();
            //读取充电站列表信息
            ChargingStationManageFxController.chargingStationManageFxController.readStationsInfo(stations);
        }else {
            alert.close();
        }
    }

    /**
     * 通过菜单子项进入代理商管理
     * @param actionEvent
     */
    public void enterAgentManageByMenuItem(ActionEvent actionEvent) {
        //弹出确认对话框询问用户
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
        alert.setHeaderText(null);
        alert.setContentText("您确定要放弃修改数据吗？");
        Optional<ButtonType> result = alert.showAndWait();
        //用户选择放弃修改数据
        if (result.get()==ButtonType.OK) {
            //进入代理商管理
            String text = agentManage.getText();
            adminSwitchoverMenu.backtrack(ModifyChargingPileDataFxController.class,text);
            //调用dao层查询代理商表
            AgentDaolmpl agentDao = new AgentDaolmpl();
            List<Agent> agents = agentDao.agentInquire();

            //读取代理商列表信息
            AgentManageFxController.agentManageFxController.readAgentInfo(agents);
        }else {
            alert.close();
        }
    }

    /**
     * 通过菜单子项进入用户管理
     * @param actionEvent
     */
    public void enterUserManageByMenuItem(ActionEvent actionEvent) {
        //弹出确认对话框询问用户
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
        alert.setHeaderText(null);
        alert.setContentText("您确定要放弃修改数据吗？");
        Optional<ButtonType> result = alert.showAndWait();
        //用户选择放弃修改数据
        if (result.get()==ButtonType.OK) {
            //进入用户管理
            String text = userManage.getText();
            adminSwitchoverMenu.backtrack(ModifyChargingPileDataFxController.class,text);
            //调用dao层查询用户表
            UserDaolmpl userDao = new UserDaolmpl();
            List<User> users = userDao.userInquire();

            //读取用户列表信息
            UserManageFxController.userManageFxController.readUserInfo(users);
        }else {
            alert.close();
        }
    }

    /**
     * 通过菜单子项进入主菜单
     * @param actionEvent
     */
    public void enterMainMenuByMenuItem(ActionEvent actionEvent) {
        //弹出确认对话框询问用户
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
        alert.setHeaderText(null);
        alert.setContentText("您确定要放弃修改数据吗？");
        Optional<ButtonType> result = alert.showAndWait();
        //用户选择放弃修改数据
        if (result.get()==ButtonType.OK) {
            //进入主菜单
            String text = mainMenu.getText();
            adminSwitchoverMenu.backtrack(ModifyChargingPileDataFxController.class,text);
        }else {
            alert.close();
        }
    }

    /**
     * 点击菜单项的退出项退出系统
     * @param actionEvent
     */
    public void clickExit(ActionEvent actionEvent) {
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
        alert.setHeaderText(null);
        alert.setContentText("您确定要退出吗？");
        Optional<ButtonType> result = alert.showAndWait();
        if (result.get() == ButtonType.OK){
//            Platform.exit();
            exitLogin.backtrack(ModifyChargingPileDataFxController.class);
        }else {
            alert.close();
        }
    }

    /**
     * 修改数据
     * @param mouseEvent
     */
    public void modifyData(MouseEvent mouseEvent) {
        //获取用户输入的数据
//        String newStationName = m_stationName.getText();
        String stringServicePower = m_servicePower.getText();
        double newServicePower = Double.parseDouble(stringServicePower);
        String stringChargingType = m_chargingType.getText();
        int newChargingType;
        if (stringChargingType.equals("快充")){
            //充电类型为快充
            newChargingType = 0;
        }else {
            //充电类型为慢充
            newChargingType = 1;
        }
//        int newChargingType = Integer.parseInt(stringChargingType);
        String stringServiceCharge = m_serviceCharge.getText();
        double newServiceCharge = Double.parseDouble(stringServiceCharge);
        int newStatus = 1;

        ChargingDaolmpl chargingDao = new ChargingDaolmpl();
        ChargingPileDaolmpl chargingPileDao = new ChargingPileDaolmpl();

        //获取用户的修改类型
        String modifyTypeText = modifyType.getText();
        if (modifyTypeText.equals("修改")) {
            //获取原来的充电桩id
            String stringOriStationPileId = oriStationPileId.getText();
            int stationPileId = Integer.parseInt(stringOriStationPileId);
            //获取新的充电站名字
//            String oriStationNameText = oriStationName.getText();
            String newStationName = m_stationName.getValue();
            //调用service层查询充电站id
            Charging chargings = chargingDao.selectStationIdByStationName(newStationName);
            long stationId = chargings.getId();

            //调用service层修改数据
            boolean condition = chargingPileDao.chargingPileModify(stationId, stationPileId, newServicePower, newChargingType, newServiceCharge, newStatus);
            if (condition){
                Alert alert = new Alert(Alert.AlertType.INFORMATION);
                alert.setHeaderText(null);
                alert.setContentText("修改成功!");
                alert.showAndWait();
            }
        }else {
            //获取新添加数据的充电站id
            String newStationName = m_stationName.getValue();
            ////调用service层查询充电站id
            Charging chargings = chargingDao.selectStationIdByStationName(newStationName);
            long stationId = chargings.getId();
            //封装实体类
            ChargingPile chargingPile = new ChargingPile(stationId, newServicePower, newChargingType, newServiceCharge, newStatus);
            //调用service层插入数据
            boolean condition = chargingPileDao.chargingPileAdd(chargingPile);
            if (condition){
                Alert alert = new Alert(Alert.AlertType.INFORMATION);
                alert.setHeaderText(null);
                alert.setContentText("新增成功!");
                alert.showAndWait();
            }
        }
        //返回充电桩查询界面，并回显数据
        //引用fxml样式
        Parent load = null;
        try {
            load = FXMLLoader.load(getClass().getResource("/fxml/admin/chargingPileManage.fxml"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        ObservableList<Stage> stageList = StageHelper.getStages();
        stageList.get(0).setScene(new Scene(load));
        //调用dao层查询充电桩表
        List<ChargingPile> chargingPiles = chargingPileDao.chargingPileInquire();

        //读取充电站列表信息
        ChargingPileManageFxController.chargingPileManageFxController.readStationPileInfo(chargingPiles);
    }

    /**
     * 选择修改后设置下拉框的选项
     * @param choices
     */
   public void setChoiceBoxData(List<String> choices){
       //添加下拉框选项
       for (int i = 0; i < choices.size(); i++) {
           m_stationName.getItems().add(choices.get(i));
       }
       //设置下拉框的默认值
       m_stationName.setValue(choices.get(0));
   }

    /**
     * 原数据回显
     * @param selectedItem
     */
    public void showData(StationPileData selectedItem){
        //正常回显
        m_stationName.setValue(selectedItem.getStationName());
        m_servicePower.setText(selectedItem.getServicePower()+"");
        m_chargingType.setText(selectedItem.getChargingType());
        m_serviceCharge.setText(selectedItem.getServiceCharge()+"");
        //设置用户选择的修改类型
        modifyType.setText("修改");
        //保存原来的充电桩id
        oriStationPileId.setText(selectedItem.getStationPileId()+"");
        //保存原来的充电站名字
        oriStationName.setText(m_stationName.getValue());
    }

    /**
     * 重置输入
     * @param mouseEvent
     */
    public void resetData(MouseEvent mouseEvent) {
        m_stationName.setValue(null);
        m_servicePower.setText("");
        m_chargingType.setText("");
        m_serviceCharge.setText("");
    }

    /**
     * 关于我们
     * @param actionEvent
     */
    public void aboutUs(ActionEvent actionEvent) {
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setHeaderText("关于我们");
        alert.setContentText("充电桩管理系统由Power-station项目组人员开发，本管理系统目前已实现四个模块的增删改查。");
        alert.showAndWait();
    }
}
